using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.SupplyChain.Purchase;


namespace HengTong.ViewModel.SupplyChain.SysPurchaseRequestBillEntryVms
{
    public partial class SysPurchaseRequestBillEntryListVm : BaseListVm<SysPurchaseRequestBillEntryView, SysPurchaseRequestBillEntrySearcher>
    {

        protected override IEnumerable<IGridColumn<SysPurchaseRequestBillEntryView>> InitGridHeader()
        {
            return new List<GridColumn<SysPurchaseRequestBillEntryView>>{
                this.MakeGridHeader(x => x.BillName),
                this.MakeGridHeader(x => x.MaterialName),
                this.MakeGridHeader(x => x.MaterialSkuName),
                this.MakeGridHeader(x => x.IsBackupMaterial),
                this.MakeGridHeader(x => x.RequestQuantity),
                this.MakeGridHeader(x => x.UnitPrice),
                this.MakeGridHeader(x => x.TotalAmount),
                this.MakeGridHeader(x => x.BudgetAmount),
                this.MakeGridHeader(x => x.TechnicalRequirement),
                this.MakeGridHeader(x => x.RequiredArrivalDate),
                this.MakeGridHeader(x => x.PurchaseProgress),
                this.MakeGridHeader(x => x.Bill),
                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<SysPurchaseRequestBillEntryView> GetSearchQuery()
        {
            var query = DC.Set<SysPurchaseRequestBillEntry>()
                .CheckEqual(Searcher.BillId, x=>x.BillId)
                .CheckContain(Searcher.BillCode, x => x.Bill.BillCode)
                .CheckEqual(Searcher.MaterialId, x=>x.MaterialId)
                .CheckContain(Searcher.MaterialCode, x => x.Material.Code)
                .CheckContain(Searcher.MaterialName, x => x.Material.Name)
                .CheckEqual(Searcher.MaterialSkuId, x=>x.MaterialSkuId)
                .CheckContain(Searcher.MaterialSkuCode, x => x.MaterialSku.Code)
                .CheckContain(Searcher.MaterialSkuName, x => x.MaterialSku.Name)
                .CheckEqual(Searcher.IsBackupMaterial, x=>x.IsBackupMaterial)
                .CheckBetween(Searcher.RequiredArrivalDate?.GetStartTime(), Searcher.RequiredArrivalDate?.GetEndTime(), x => x.RequiredArrivalDate, includeMax: false)
                .CheckEqual(Searcher.PurchaseProgress, x=>x.PurchaseProgress)
                .CheckContain(Searcher.Applicant, x=>x.Bill.Applicant)
                .CheckBetween(Searcher.RequestDate?.GetStartTime(), Searcher.RequestDate?.GetEndTime(), x => x.Bill.RequestDate, includeMax: false)
                .CheckBetween(Searcher.NeedDate?.GetStartTime(), Searcher.NeedDate?.GetEndTime(), x => x.Bill.NeedDate, includeMax: false)
                .CheckEqual(Searcher.PurchaseType, x=>x.Bill.PurchaseType)
                .CheckEqual(Searcher.MaterialType, x=>x.Bill.MaterialType)
                .CheckEqual(Searcher.UrgencyLevel, x=>x.Bill.UrgencyLevel)
                .CheckContain(Searcher.Approver, x=>x.Bill.Approver)
                .CheckBetween(Searcher.ApprovalTime?.GetStartTime(), Searcher.ApprovalTime?.GetEndTime(), x => x.Bill.ApprovalTime, includeMax: false)
                .CheckEqual(Searcher.GroupId, x=>x.Bill.GroupId)
                .CheckEqual(Searcher.OrgId, x=>x.Bill.OrgId)
                .CheckContain(Searcher.BillCode, x=>x.Bill.BillCode)
                .CheckEqual(Searcher.BillStatus, x=>x.Bill.BillStatus)
                .CheckEqual(Searcher.WorkflowStatus, x=>x.Bill.WorkflowStatus)
                .CheckContain(Searcher.Remark, x=>x.Bill.Remark)
                .Select(x => new SysPurchaseRequestBillEntryView
                {
				    ID = x.ID,
                    BillName = x.Bill.BillCode,
                    MaterialName = x.Material.Name,
                    MaterialSkuName = x.MaterialSku.Name,
                    IsBackupMaterial = x.IsBackupMaterial,
                    RequestQuantity = x.RequestQuantity,
                    UnitPrice = x.UnitPrice,
                    TotalAmount = x.TotalAmount,
                    BudgetAmount = x.BudgetAmount,
                    TechnicalRequirement = x.TechnicalRequirement,
                    RequiredArrivalDate = x.RequiredArrivalDate,
                    PurchaseProgress = x.PurchaseProgress,
                    Bill = x.Bill
                })
                .OrderByDescending(x => x.ID);
            return query;
        }
    }

    public class SysPurchaseRequestBillEntryView : SysPurchaseRequestBillEntry{
        [Display(Name = "单据编号")]
        public String BillName { get; set; }
        [Display(Name = "名称")]
        public String MaterialName { get; set; }
        [Display(Name = "名称")]
        public String MaterialSkuName { get; set; }

    }
}
